package com.example.whm;



import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.whm.MyPreOrderFragment.adapter;
import com.example.whm.OneFragmentNormal.PageAdapter;
import com.example.whm.model.Carousel;
import com.example.whm.model.GalleryModel;
import com.example.whm.model.IndexP;
import com.example.whm.model.PreOrder;
import com.example.whm.model.ZaoDian;

public class OneFragmentRank extends Fragment {

	private AQuery aq;
	private AQuery aq2;
	
	private String topic;
	IndexP index;
	View view;
	ListView list;
	private ViewPager pager_new;
	private ViewPager pager_hot;
	private ViewPager pager_recommand;
	
	

    @Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
 			Bundle savedInstanceState) {
     	 view = inflater.inflate(R.layout.one_layout_rank, container, false);  
     	 pager_new = (ViewPager)view.findViewById(R.id.one_layout_new_gallery);
     	 pager_hot = (ViewPager)view.findViewById(R.id.one_layout_hot_gallery);
     	 pager_recommand = (ViewPager)view.findViewById(R.id.one_layout_recommend_gallery);
     	 
     	 
    	aq=new AQuery(getActivity(), view);
    	aq2=new AQuery(getActivity(), view);
        index = JSONObject.parseObject(getArguments().getString("data"), IndexP.class);
     	return view;
 	}
    
    



	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	
		FragmentManager fm = getChildFragmentManager();
		
		  if (index.getMap().containsKey("time")) {
			  aq.find(R.id.one_layout_new).visible();
			  List<GalleryModel> l=list2gm(index.getMap().get("time"));
			  aq.find(R.id.one_layout_text_new_label).text(String.valueOf(l.size()));
			  aq.find(R.id.one_layout_text_new_select_label).text(String.valueOf(1));
			  //PageAdapter adapter = new PageAdapter(fm,l,pager_new.getId());
			  pager_new.setAdapter(new PageAdapter(fm,l,pager_new.getId()));	
			 
			 // aq.find(R.id.one_layout_new_gallery).adapter(new adapter(l));
			 //  aq.find(R.id.one_layout_new_gallery).itemSelected(OneFragmentRank.this);
			   
			//   aq.find(R.id.one_layout_zaodian_gallery).adapter(new adapter(list2gm(index.getMap().get("zaodian"))));
			 //  aq.find(R.id.one_layout_zaodian_gallery).itemSelected(OneFragmentNormal.this);
		  }
		   if (index.getMap().containsKey("hot")) {
			   aq.find(R.id.one_layout_hot).visible();
			   List<GalleryModel> l=list2gm(index.getMap().get("hot"));
				aq.find(R.id.one_layout_text_hot_label).text(String.valueOf(l.size()));
				aq.find(R.id.one_layout_text_hot_select_label).text(String.valueOf(1));
				pager_hot.setAdapter(new PageAdapter(fm,l,pager_hot.getId()));	
			   //aq.find(R.id.one_layout_hot_gallery).itemSelected(OneFragmentRank.this);
		  }
		   if (index.getMap().containsKey("recommand")) {
			   aq.find(R.id.one_layout_recommand).visible();
			   List<GalleryModel> l=list2gm(index.getMap().get("recommand"));
			   aq.find(R.id.one_layout_text_recommend_label).text(String.valueOf(l.size()));
				aq.find(R.id.one_layout_text_recommend_select_label).text(String.valueOf(1));
			   pager_recommand.setAdapter(new PageAdapter(fm,l,pager_recommand.getId()));	
			  // aq.find(R.id.one_layout_recommend_gallery).itemSelected(OneFragmentRank.this);
		   }
		
		
		
		
		
		
		  
		
		  
		  // aq.find(R.id.one_layout_people_gallery).adapter(new adapter(l));
		  // aq.find(R.id.one_layout_people_gallery).itemSelected(OneFragmentRank.this);
		  // aq.find(R.id.one_layout_xiaochi_gallery).adapter(new adapter(l));
		  // aq.find(R.id.one_layout_tianping_gallery).adapter(new adapter(l));
		
		//init();
		
	}

	public List<GalleryModel> list2gm(List<ZaoDian> l){
		List<GalleryModel> list=new ArrayList<GalleryModel>();
		
		int size=l.size(); 
		GalleryModel g = null;
		
		for (int i = 1; i <= size; i++) {
			if (i%2==1) { 
				g=new GalleryModel();
				g.setFirst(l.get(i-1)); 
				if (i==size) {
					list.add(g);
				}
				
			}else{
				g.setLast(l.get(i-1));	
				list.add(g);
			}
			
		}
		
		return list;
		
	}
	
	
	public  class PageAdapter extends FragmentStatePagerAdapter{

		List<GalleryModel> data;
		int id;
		public PageAdapter(FragmentManager fm,List<GalleryModel> da,int id) {
			super(fm);
			this.data=da; 
			this.id=id; 
		}
       
		
		@Override
		public Fragment getItem(int pos) {
			
			//AQUtility.debug("primary", topics[pos]);
			//TwoFragmentItem f=new TwoFragmentItem();
			Bundle args = new Bundle();
			args.putSerializable("gallery", data.get(pos)); 
		/*	args.putString("id", data.get(pos).getId());
			args.putInt("pos", pos);
			if (pos==0) {
				
				args.putString("result", result);
			}*/
			OneFragmentGallery fragment = (OneFragmentGallery) Fragment.instantiate(OneFragmentGallery.class, args);			
			return fragment;
			
		}


		@Override
		public void setPrimaryItem(ViewGroup container, int position,
				Object object) {
			// TODO Auto-generated method stub
		    set_tag(position, id); 
			super.setPrimaryItem(container, position, object);
		}


		@Override
		public int getCount() {
			return data.size();
		}
		

		
		
	}		
	
/*
public void init(){
	
	  String url="http://"+Whm_app.api_url+":9000/api/user_preorder";
	  AjaxCallback<String> cb = new AjaxCallback<String>();        
	  cb.url(url).type(String.class).weakHandler(this, "ui");
	  SharedPreferences sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
  	String cookiename=sp.getString("cookie_name",null);
  	String cookievalue=sp.getString("cookie_value",null);
  	if (cookiename!=null&&cookievalue!=null) {
  		cb.cookie(cookiename,cookievalue);               
	}
	  aq.ajax(cb);
  }

  public void ui(String url, String r, AjaxStatus status){
	
	  System.out.println("ui  s="+r); 
	  if (status.getCode()==200) {
		 
		  
		  final String s=r;
		  new Thread( new Runnable() {
			  @Override
			public void run() {
				  // String s1= JSON.parseObject(s).getString("users");
				  
				  String mapr= JSON.parseObject(s).getString("mapr");
				  String listj= JSON.parseObject(s).getString("list");
				  map= JSON.parseObject(mapr, Map.class);
				  
				  boxs= JSON.parseArray(listj, PreOrder.class);
				  System.out.println("boxs.size()========="+boxs.size());
				
				  getActivity().runOnUiThread(new Runnable() {
					  
					  @Override
					  public void run() {
						  Toast.makeText(getActivity(), "boxs.size()="+boxs.size(), 1000).show();
						  if (isload) {
							  show_dialog(false);
							 
							  adap.setList(boxs);
							  adap.notifyDataSetChanged();
						}else{		
							isload=true;
						  aq2.id(R.id.progress).gone();
						//  aq2.id(R.id.four_layout_line).visible();
						  
						//   list=(ListView) aq2.id(R.id.myorder_fragment_listview).getListView();
						  View v=getActivity().getLayoutInflater().inflate(R.layout.four_layout_header, null);
						  View v2=getActivity().getLayoutInflater().inflate(R.layout.four_layout_footer, null);
						   AQuery a=new AQuery(v);
						  AQuery a2=new AQuery(v2);
						  a.id(R.id.four_layout_contact_name).text(add.getContact_name());
						  a.id(R.id.four_layout_contact_phone).text(add.getPhone());
						  a.id(R.id.four_layout_contact_addr).text(add.getDetail_address());
						  a2.find(R.id.four_layout_btn_sure).clicked(MyOrderFragment.this); 
						   radioButtonGroup=(RadioGroup) a.find(R.id.four_layout_pay).getView();
						  list.addHeaderView(v);
						  list.addFooterView(v2);
						 
						  adap=new adapter(boxs);
						  aq2.id(R.id.myorder_fragment_listview).adapter(adap);
						  aq2.id(R.id.myorder_fragment_listview).itemClicked(MyPreOrderFragment.this);
						  aq2.id(R.id.myorder_fragment_listview).getListView().setOnItemLongClickListener(MyPreOrderFragment.this);
						
						
						
						
						}
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  //Utility.setListViewHeightBasedOnChildren(list);
					  }
				  });
			  }
		  }).start();
			
		  
	}else{
		Toast.makeText(getActivity(), "request error", 1000).show();
		
	}
	  
  }

*/






public void set_tag(int arg2,int id){
	int index=arg2+1;
	switch (id) { 
	case R.id.one_layout_new_gallery:	
		aq2.find(R.id.one_layout_text_new_select_label).text(String.valueOf(index));
		break;
	case R.id.one_layout_hot_gallery:
		//aq.find(R.id.one_layout_text_hot_label).text(String.valueOf(count));
		aq2.find(R.id.one_layout_text_hot_select_label).text(String.valueOf(index));
		break;
	case R.id.one_layout_recommend_gallery:
		//aq.find(R.id.one_layout_text_recommend_label).text(String.valueOf(count));
		aq2.find(R.id.one_layout_text_recommend_select_label).text(String.valueOf(index));
		break;
	case R.id.one_layout_people_gallery:
		//aq.find(R.id.one_layout_text_people_label).text(String.valueOf(count));
		aq2.find(R.id.one_layout_text_people_select_label).text(String.valueOf(index));
		break;
	default:
		break;
	}
}


    
    
    
 
}
